local manyi = fk.CreateSkill{
  name = "xiaobai__manyi"
}

Fk:loadTranslationTable{
  ["xiaobai__manyi"] = "蛮异",
  [":xiaobai__manyi"] = "结束阶段，你可以选择一项：1.对一名与你本回合开始时势力相同但此时不同的角色造成1点伤害；"..
    "2.令一名与你此时势力相同的角色摸两张牌；背水：你不能因“叛探”变更至本回合开始时的势力。",

  ["#xiaobai__manyi_ask"] = "蛮异：你可以选择一名角色，或选择两名不同角色背水：你不能因“叛探”变更至本回合开始时的势力。",
  ["@xiaobai__manyi_prohibit_change"] = "蛮异禁止",
  ["@xiaobai__manyi-turn"] = "蛮异",
  ["xiaobai__manyi_hurt"] = "造成伤害",
  ["xiaobai__manyi_draw"] = "摸两张牌",

  ["$xiaobai__manyi1"] = "蛮兵勇武，实令吾钦佩，愿献此城于蛮王。",
  ["$xiaobai__manyi2"] = "臣已知错，请丞相开恩，命臣再效于汉。",
}

manyi:addEffect(fk.EventPhaseStart, {
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(self) and player.phase == Player.Finish
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local done, data = room:askToUseActiveSkill(player, {
      skill_name = "xiaobai__manyi_active",
      no_indicate = false,
      prompt = "#xiaobai__manyi_ask",
      extra_data = {
        skillName = "xiaobai__manyi"
      }
    })
    if done and data then
      event:setCostData(self, data.targets)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self) ---@type ServerPlayer[]
    for _, to in ipairs(tos) do
      if to.kingdom == player.kingdom then
        to:drawCards(2, manyi.name)
      else
        room:damage{
          from = player,
          to = to,
          damage = 1,
          skillName = manyi.name
        }
      end
    end
    if player.dead then return end

    if #tos == 2 then
      room:addTableMark(player, "@xiaobai__manyi_prohibit_change", player:getMark("@xiaobai__manyi-turn"))
    end
  end
})

manyi:addEffect(fk.TurnStart, {
  can_refresh = function (self, event, target, player, data)
    return player == target and player:hasSkill(self, true, true)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@xiaobai__manyi-turn", player.kingdom)
  end
})

manyi:addAcquireEffect(function (self, player)
  if #player.room.logic:getEventsOfScope(GameEvent.ChangeProperty, 1, function (e)
    player.room:setPlayerMark(player, "@xiaobai__manyi-turn", e.data.kingdom)
    return true
  end, Player.HistoryTurn) == 0 then
    player.room:setPlayerMark(player, "@xiaobai__manyi-turn", player.kingdom)
  end
end)

local manyi_active = fk.CreateSkill{
  name = "xiaobai__manyi_active"
}

manyi_active:addEffect("active", {
  target_tip = function (self, player, to_select, selected, selected_cards, card, selectable, extra_data)
    if to_select.kingdom == player:getMark("@xiaobai__manyi-turn") and to_select.kingdom ~= player.kingdom then
      return "xiaobai__manyi_hurt"
    elseif to_select.kingdom == player.kingdom then
      return 'xiaobai__manyi_draw'
    end
  end,
  target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
    if #selected == 0 then return ((to_select.kingdom == player:getMark("@xiaobai__manyi-turn") and player.kingdom ~= to_select.kingdom) or to_select.kingdom == player.kingdom) end
    if #selected == 1 then
      local t = selected[1]
      if t.kingdom == player.kingdom then
        return to_select.kingdom == player:getMark("@xiaobai__manyi-turn") and player.kingdom ~= to_select.kingdom
      else
        return to_select.kingdom == player.kingdom
      end
    end
  end,
  card_filter = Util.FalseFunc,
})

local skels = {}

table.insert(skels, manyi)
table.insert(skels, manyi_active)

return skels